﻿using System;
using System.Collections.Generic;

namespace School.API
{
    using EDP.Common.Model;

    using School.Model;
    using School.Business;
    using EDP.Common;

    /// <summary>
    /// 学生信息接口
    /// </summary>
    public class StudentAPI : WebApiBase
    {
        private readonly StudentBLL bll_Student = StudentBLL.Create();

        /// <summary>
        /// 保存学生信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<bool> SaveStudent(StudentSaveModel param) {
            StudentModel item = new StudentModel();
            param.CopyTo(item);
            return this.bll_Student.SaveStudent(item.ID, item);
        }

        /// <summary>
        /// 新增学生信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<bool> AddStudent(StudentSaveModel param) {
            StudentModel item = new StudentModel();
            param.CopyTo(item);
            return this.bll_Student.AddStudent(item);
        }

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<bool> UpdateStudent(StudentSaveModel param) {
            StudentModel item = new StudentModel();
            param.CopyTo(item);
            return this.bll_Student.UpdateStudent(item);
        }

        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<bool> DeleteStudent(IdModel param) {
            return this.bll_Student.DeleteStudent(param.ID);
        }

        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<StudentResultModel> GetStudent(IdModel param) {
            ResultModel<StudentResultModel> ret = new ResultModel<StudentResultModel>();
            ret.Content = new StudentResultModel();

            try {
                var result = this.bll_Student.GetStudent(param.ID);
                if (result.ErrorCode == 0)
                    result.Content.CopyTo(ret.Content);
                else {
                    ret.ErrorLevel = result.ErrorLevel;
                    ret.ErrorCode = result.ErrorCode;
                    ret.Message = result.Message;
                }
            }
            catch (Exception e) {
                ret.ErrorLevel = (int)ErrorLevelEnum.SystemError;
                ret.ErrorCode = (int)ErrorCodeEnum.SystemError;
                ret.Message = e.Message;
            }

            return ret;
        }

        /// <summary>
        /// 获取学生信息列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<List<StudentResultModel>> GetStudentList(StudentQueryInputModel param) {
            ResultModel<List<StudentResultModel>> ret = new ResultModel<List<StudentResultModel>>();
            ret.Content = new List<StudentResultModel>();

            StudentQueryModel queryModel = new StudentQueryModel();
            param.CopyTo(queryModel);

            try {
                var result = this.bll_Student.GetStudentList(queryModel);
                if (result.ErrorCode == 0) {
                    result.Content.CopyTo(ret.Content);
                    ret.Page = result.Page;
                }
                else {
                    ret.ErrorLevel = result.ErrorLevel;
                    ret.ErrorCode = result.ErrorCode;
                    ret.Message = result.Message;
                }
            }
            catch (Exception e) {
                ret.ErrorLevel = (int)ErrorLevelEnum.SystemError;
                ret.ErrorCode = (int)ErrorCodeEnum.SystemError;
                ret.Message = e.Message;
            }

            return ret;
        }

        /// <summary>
        /// 获取组织列表
        /// </summary>
        /// <returns></returns>
        public ResultModel<List<OrganizationResultModel>> GetOrganizationList() {
            ResultModel<List<OrganizationResultModel>> ret = new ResultModel<List<OrganizationResultModel>>();
            ret.Content = new List<OrganizationResultModel>();

            try {
                var result = this.bll_Student.GetOrganizationList();
                if (result.ErrorCode == 0) {
                    result.Content.CopyTo(ret.Content);
                    ret.Page = result.Page;
                }
                else {
                    ret.ErrorLevel = result.ErrorLevel;
                    ret.ErrorCode = result.ErrorCode;
                    ret.Message = result.Message;
                }
            }
            catch (Exception e) {                
                ret.ErrorLevel = (int)ErrorLevelEnum.SystemError;
                ret.ErrorCode = (int)ErrorCodeEnum.SystemError;
                ret.Message = e.Message;
            }

            return ret;
        }

        /// <summary>
        /// 获取组织
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ResultModel<OrganizationResultModel> GetOrganization(IdModel param) {
            ResultModel<OrganizationResultModel> ret = new ResultModel<OrganizationResultModel>();
            ret.Content = new OrganizationResultModel();

            try {
                var result = this.bll_Student.GetOrganization(param.ID);
                if (result.ErrorCode == 0)
                    result.Content.CopyTo(ret.Content);
                else {
                    ret.ErrorLevel = result.ErrorLevel;
                    ret.ErrorCode = result.ErrorCode;
                    ret.Message = result.Message;
                }
            }
            catch (Exception e) {
                ret.ErrorLevel = (int)ErrorLevelEnum.SystemError;
                ret.ErrorCode = (int)ErrorCodeEnum.SystemError;
                ret.Message = e.Message;
            }

            return ret;
        }
    }
}
